<?php

 $template->setTemplateFile('ajout_commune_form','tpl/ajout_commune_form.tpl');
 
if(isset($_POST['cp']) && isset($_POST['nomc'])) {
	$cp = $_POST['cp'];
	$nomc = sql_protect(html_escape($_POST['nomc']));
	$ress = SGBD::query(
		"INSERT INTO commune VALUES ('$cp','$nomc');" );
	header("Location: index.php?page=admin_communes");
}
	
if(isset($_GET['delete_commune'])) {
	$cp = $_GET['delete_commune'];
	$res = SGBD::query(
		"DELETE FROM commune
		WHERE cpcommune = '$cp'; " );
}

	$result = SGBD::query(
			"SELECT * FROM commune ORDER BY cpcommune;"
			);
	if(!$result)
		throw new Exception("Erreur lors de la requête SQL " . $result);	
		
	// Extraction des donnees   
	// ... et Creation d'un template vide qui va recevoir tous les spectacles recherchés
	$template->setTemplateFile('show_list_admin_commune',NULL); 

	while($data = SGBD::fetch($result)){

		$template->setTemplateFile('show_info_gestion_commune','tpl/show_info_gestion_commune.tpl');
		$template->parseTemplate('show_info_gestion_commune',"COMMUNE_CP",$data['cpcommune']);
		$template->parseTemplate('show_info_gestion_commune',"COMMUNE_NOM",$data['nomcommune']);
		$template->mergeTemplates('show_info_gestion_commune','show_list_admin_commune');
	}
	$template->mergeTemplates('show_list_admin_commune','ajout_commune_form');
	$template->setTemplateFile('center','tpl/center.tpl');
	$template->parseTemplate('center',"CENTER_CONTENT",$template->getTemplate('ajout_commune_form'));

?>
